<?php
class NodeAction extends CommonAction{
	public function index() {
		$this->display();
	}
	public function data() {
		$n = new NodeModel();
		$method = $_POST;
		switch($method['do']){
			case 'menu':
				if($method['tree'] == 'tree') $n->field('title as text,id');
				if(!empty($method['p']) && $method['p']<>'menu')
				{
					if($method['tree'] == 'tree') echo json_encode($n->where('pid = '.$method['p'])->select());
					else echo json_encode(array('list'=>$n->where('pid = '.$method['p'])->select()));
				}
				else
				{
					if($method['tree'] == 'tree') echo json_encode($n->where('pid = 0')->select());
					else echo json_encode(array('list'=>$n->where('pid = 0')->select()));
				}
				break;
			case 'groupid':
				$g = new GroupModel();
				$groupid = $g->field('id,title')->select();
				$groupid[] = array('title'=>'不分组','id'=>0);
				echo json_encode($groupid);
				break;
			case 'typeid':
				$a = $n->where('group_id = '.$method['groupid'])->select();
				if(!$a) $a = array();
				echo json_encode(array('list'=>$a));
				break;
		}
	}
	public function add() {
		if($this->isPost()){
			$method = $_POST;
			$array = array();
			$nodeObj = new NodeModel();
			$data['title'] = $method['title'];
			$data['name'] = $method['name'];
			$data['group_id'] = $method['group_id'];
			$data['status'] = $method['status'];
			$data['intro'] = $method['intro'];
			$data['pid'] = $method['pid'];
			$node = $nodeObj->where(array('id'=>$data['pid']))->find();
			$data['level'] = $node['level'] + 1;
			if($nodeObj->add($data))
			{
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}
	public function edit() {
		if($this->isPost()){
			$method = json_decode(file_get_contents("php://input"))->list;
			$array = array();
			$nodeObj = new NodeModel();
			//$node = $nodeObj->where(array('id'=>$method->pid))->find();
			//$nodeObj->level = $node['level'] + 1;
			foreach($method as $k => $v)
			{
				$nodeObj->$k = $v;
			}
			if($nodeObj->save())
			{
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}
	public function delete() {
		if($this->isPost())
		{
			$method = json_decode(file_get_contents("php://input"))->list;
			$data = array();
			$nodeObj = new NodeModel();
			$map['id'] = $method;
			if($nodeObj->where($map)->delete()){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}
	protected function ajaxReturn($data,$info='',$status=1,$type=''){
        // 保证AJAX返回后也能保存日志
        if(C('LOG_RECORD')) Log::save();
        $result  =  array();
        $result['success']  =  $status;
        $result['info'] =  $info;
        $result['data'] = $data;
        if(empty($type)) $type  =   C('DEFAULT_AJAX_RETURN');
        if(strtoupper($type)=='JSON') {
            // 返回JSON数据格式到客户端 包含状态信息
            header("Content-Type:text/html; charset=utf-8");
            exit(json_encode($result));
        }elseif(strtoupper($type)=='XML'){
            // 返回xml格式数据
            header("Content-Type:text/xml; charset=utf-8");
            exit(xml_encode($result));
        }elseif(strtoupper($type)=='EVAL'){
            // 返回可执行的js脚本
            header("Content-Type:text/html; charset=utf-8");
            exit($data);
        }else{
            // TODO 增加其它格式
        }

	}
}